<template>
  <div>
    <el-row :gutter="40" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="onClickGotoSection('newVisitis')">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="peoples" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              用户数
            </div>
            <count-to :start-val="0" :end-val="customers" :duration="2600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="onClickGotoSection('toTraded')">
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon icon-class="message" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              已成交订单
            </div>
            <count-to :start-val="0" :end-val="orderDone" :duration="3000" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="onClickGotoSection('toDelivery')">
          <div class="card-panel-icon-wrapper icon-shopping">
            <svg-icon icon-class="shopping" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              待发货
            </div>
            <count-to :start-val="0" :end-val="orderUnDelivery" :duration="3600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <el-card>
          <div class="chart-header" slot="header">
            <div class="left">
              <svg-icon icon-class="chart" style="color: #1890ff;" />
              <span class="title">订单</span>
            </div>
            <div class="right">
              <el-radio-group v-model="timeType" @change="onChange">
                <el-radio-button label="1">3天</el-radio-button>
                <el-radio-button label="2">1周</el-radio-button>
                <el-radio-button label="3">1月</el-radio-button>
              </el-radio-group>
            </div>
          </div>
          <div class="chart-content">
            <div id="chart_order"></div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import CountTo from 'vue-count-to';
import dayjs from 'dayjs';

export default {
  components: {
    CountTo,
  },
  data() {
    return {
      customers: 0, // 用户数
      orderUnDelivery: 0, // 待发货数
      orderDone: 0, // 已成交订单

      myChart: null,
      timeType: '2',
      xAxisData: [], // x轴时间

      yAxisAmount: {
        // y轴订单金额
        data: [],
        max: null,
        min: null,
        interval: null,
      },

      yAxisCount: {
        // y轴订单数量
        data: [],
        max: null,
        min: null,
        interval: null,
      },
    };
  },
  mounted() {
    this.init();
    window.onresize = () => {
      this.redraw();
    };
  },
  methods: {
    init() {
      this.getHomeShowList();
      this.getDaily();
    },

    onClickGotoSection(type) {
      switch (type) {
        case 'toDelivery':
          this.$router.push({ path: '/market/order?s=3' });
          break;
        case 'toTraded':
          this.$router.push({ path: '/market/order?s=9' });
          break;
        default:
          break;
      }
    },

    onChange() {
      this.getDaily();
    },

    drawChart() {
      const chartDom = document.getElementById('chart_order');
      this.myChart = echarts.init(chartDom);

      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999',
            },
          },
        },
        toolbox: {
          show: true,
          feature: {
            saveAsImage: { show: true },
          },
        },
        legend: {
          data: ['订单金额', '订单数'],
        },
        xAxis: [
          {
            type: 'category',
            data: this.xAxisData,
            axisPointer: {
              type: 'shadow',
            },
          },
        ],
        yAxis: [
          {
            type: 'value',
            name: '金额',
            // min: 0,
            // max: this.yAxisAmount.max,
            // interval: this.yAxisAmount.interval,
            minInterval: 1,
          },
          {
            type: 'value',
            name: '数量',
            // min: 0,
            // max: this.yAxisCount.max,
            // interval: this.yAxisCount.interval,
            minInterval: 1,
          },
        ],
        series: [
          {
            name: '订单金额',
            type: 'bar',
            data: this.yAxisAmount.data,
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#83bff6' },
                { offset: 0.5, color: '#188df0' },
                { offset: 1, color: '#188df0' },
              ]),
            },
          },
          {
            name: '订单数',
            type: 'line',
            yAxisIndex: 1,
            data: this.yAxisCount.data,
            itemStyle: {
              color: '#6fdeab',
            },
          },
        ],
      };

      option && this.myChart.setOption(option);
    },

    redraw() {
      this.myChart.resize();
    },

    setDateRange() {
      const nowDate = dayjs().format('YYYY-MM-DD');
      let value, type;
      switch (this.timeType) {
        case '1':
          value = 3;
          type = 'day';
          break;

        case '2':
          value = 7;
          type = 'day';
          break;

        case '3':
          value = 1;
          type = 'month';
          break;

        default:
          break;
      }

      const beginDate = dayjs().subtract(value, type).format('YYYY-MM-DD');
      const endDate = nowDate;
      return [beginDate, endDate];
    },

    async getHomeShowList() {
      const params = this.$store.getters.customerId;
      const { data } = await this.$api.getHomeShowList(params);
      this.customers = data.customers;
      this.orderUnDelivery = data.orderUnDelivery;
      this.orderDone = data.orderDone;
      this.$store.dispatch('order/setUnDelivery', data.orderUnDelivery);
    },

    async getDaily() {
      this.xAxisData = [];
      this.yAxisAmount = { data: [], max: null, min: null, interval: null };
      this.yAxisCount = { data: [], max: null, min: null, interval: null };

      const [beginDate, endDate] = this.setDateRange();
      const params = { beginDate, endDate };
      const { success, data = [] } = await this.$api.getDaliy(params);

      if (success) {
        data.forEach((e) => {
          this.xAxisData.push(e.orderDate);
          this.yAxisAmount.data.push(e.saleAmount);
          this.yAxisCount.data.push(e.orderCount);
        });

        // this.calcChartData();
        this.drawChart();
      }
    },

    calcChartData() {
      this.yAxisAmount.max = Math.max.apply(Math, this.yAxisAmount.data);
      this.yAxisAmount.min = Math.min.apply(Math, this.yAxisAmount.data);
      this.yAxisAmount.interval = Math.ceil(
        (this.yAxisAmount.max - this.yAxisAmount.min) / 10
      );

      this.yAxisCount.max = Math.max.apply(Math, this.yAxisCount.data);
      this.yAxisCount.min = Math.min.apply(Math, this.yAxisCount.data);
      this.yAxisCount.interval = 10;

      this.yAxisAmount.max = Math.ceil(
        this.yAxisAmount.max + this.yAxisAmount.interval
      );

      this.yAxisCount.max = Math.ceil(
        this.yAxisCount.max + this.yAxisCount.interval
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3;
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3;
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;

  .left {
    .title {
      margin-left: 5px;
    }
  }
}
.chart-content {
  #chart_order {
    width: 100%;
    height: 550px;
  }
}

@media (max-width: 550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
::v-deep {
  .el-radio-button:first-child .el-radio-button__inner {
    border: none;
  }
  .el-radio-button__inner {
    border: none;
  }
  .el-radio-button__orig-radio:checked + .el-radio-button__inner {
    color: #409eff;
    background-color: #fff;
    box-shadow: none;
  }
  .el-radio-button__inner {
    border-radius: 0;
    padding: 12px;
  }
}
</style>
